package org.dreamwork.jasmine2.engine;

import org.apache.log4j.Logger;
import org.dreamwork.util.StringUtil;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.pdf.ITextOutputDevice;
import org.xhtmlrenderer.pdf.ITextUserAgent;
import org.xhtmlrenderer.resource.CSSResource;
import org.xhtmlrenderer.resource.ImageResource;
import org.xhtmlrenderer.resource.XMLResource;

import java.awt.*;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: seth.yang
 * Date: 2010-10-12
 * Time: 18:47:38
 */
public class URLUserAgent extends ITextUserAgent {
    private String base;
    private String context;
    private SharedContext sharedContext;

    private String home;

    private static final Logger logger = Logger.getLogger (URLUserAgent.class);

    public URLUserAgent (ITextOutputDevice outputDevice) {
        super (outputDevice);

        home = System.getProperty ("user.dir");
        if (home.indexOf ('\\') != -1) home = home.replace ('\\', '/');
        if (home.charAt (0) == '/')  home = home.substring (1);
        if (home.charAt (home.length () - 1) != '/') home += '/';
        home = "file:/" + home;
        logger.debug ("home = " + home);
    }

    private String processURI (String uri) {
        logger.debug ("processing uri: " + uri);
        if (StringUtil.isEmpty (uri)) return null;
        String lower = uri.toLowerCase ();
        String ret;
        if (uri.startsWith (home)) {
            ret = base + uri.substring (home.length ());
        } else if (lower.startsWith ("/")) {
            ret = context + uri;
        } else if (lower.startsWith ("http://") || lower.startsWith ("https://") || lower.startsWith ("ftp://")) {
            ret = uri;
        } else if (lower.startsWith ("file:/")) {
            File file = new File (uri);
            if (file.canRead ())
                ret = uri;
            else if (uri.contains ("InternalResource.jxd")) {
                int pos = uri.indexOf (context);
                ret = base + uri.substring (pos + context.length () + 1);
            } else {
                ret = base + uri;
            }
        } else {
            ret = base + uri;
        }
        logger.debug ("returnning uri: " + ret);
        return ret;
    }

    public String getBase () {
        return base;
    }

    public void setBase (String base) {
        this.base = base;
    }

    public String getContext () {
        return context;
    }

    public void setContext (String context) {
        this.context = context;
    }

    @Override
    public void setSharedContext (SharedContext sharedContext) {
        this.sharedContext = sharedContext;
        super.setSharedContext (sharedContext);
    }

    @Override
    public SharedContext getSharedContext () {
        return sharedContext;
    }

    @Override
    public ImageResource getImageResource (String uri) {
        uri = processURI (uri);
        return super.getImageResource (uri);
    }

    @Override
    public CSSResource getCSSResource (String uri) {
        uri = processURI (uri);
        return super.getCSSResource (uri);
    }

    @Override
    public XMLResource getXMLResource (String uri) {
        return super.getXMLResource (uri);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public byte[] getBinaryResource (String uri) {
        return super.getBinaryResource (uri);    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void shrinkImageCache () {
        super.shrinkImageCache ();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    public void clearImageCache () {
        super.clearImageCache ();    //To change body of overridden methods use File | Settings | File Templates.
    }

    @Override
    protected ImageResource createImageResource (String uri, Image img) {
        return super.createImageResource (uri, img);    //To change body of overridden methods use File | Settings | File Templates.
    }
}
